<html>
<head><meta charset="utf-8"><title>runtime liveness guarantees · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html">runtime liveness guarantees</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226575693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226575693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226575693">(Feb 16 2021 at 21:00)</a>:</h4>
<p>I'm curious about what (if any) guarantees are given about the liveness of references at runtime. Consider the function below:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="c1">// Lots of other unrelated code goes here...</span>

<span class="w">    </span><span class="n">x</span><span class="p">.</span><span class="n">bar</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Assuming that if statically we've determined that the reference to <code>x</code> must live until the end of the call to <code>foo</code>, is the runtime going to ensure it can never die early? If Rust (or LLVM) realises that the reference on the stack (or in a register) can be overwritten early [because it's no longer used] -will it? To put another way: if semantically <code>x</code> lives until the end of <code>foo</code>, can I be sure that for the duration of <code>foo</code>'s stack frame, there will always be a valid reference to <code>x</code> either in a register, or on its frame?</p>



<a name="226580768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226580768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226580768">(Feb 16 2021 at 21:39)</a>:</h4>
<p>No, the compiler is free to remove the heap allocation and any <code>&amp;self</code> parameter of <code>bar</code> if it doesn't change the observable behavior of the program</p>



<a name="226581279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226581279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226581279">(Feb 16 2021 at 21:43)</a>:</h4>
<p><span class="user-mention" data-user-id="223286">@Jake Hughes</span> See this codegen test for a simple demo: <a href="https://github.com/rust-lang/rust/blob/master/src/test/codegen/alloc-optimisation.rs">https://github.com/rust-lang/rust/blob/master/src/test/codegen/alloc-optimisation.rs</a></p>



<a name="226587198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226587198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226587198">(Feb 16 2021 at 22:16)</a>:</h4>
<p>I see. And this is also true if the heap allocation wasn't removed though: is the compiler obliged to keep a reference to <code>x</code> around until the end of <code>foo</code> in that case, or can it be removed early if it doesn't change the observable behaviour?</p>



<a name="226587777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226587777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226587777">(Feb 16 2021 at 22:22)</a>:</h4>
<p>The compiler is allowed to do anything to the program as long as the observable behavior is preserved</p>



<a name="226590487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226590487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226590487">(Feb 16 2021 at 22:49)</a>:</h4>
<p>(And registers and stack frames and such aren't deemed observable, because that would be a huge restriction on the optimizer.)</p>



<a name="226591370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226591370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226591370">(Feb 16 2021 at 22:58)</a>:</h4>
<p>Thank you both, this is really useful.</p>



<a name="226600636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/runtime%20liveness%20guarantees/near/226600636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/runtime.20liveness.20guarantees.html#226600636">(Feb 17 2021 at 00:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/runtime.20liveness.20guarantees/near/226590487">said</a>:</p>
<blockquote>
<p>(And registers and stack frames and such aren't deemed observable, because that would be a huge restriction on the optimizer.)</p>
</blockquote>
<p>Barring uses of inline assembly though.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>